from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.
class BaseModel(models.Model):
    """为模型类补充字段"""
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True  # 说明是抽象模型类, 用于继承使用，数据库迁移时不会创建BaseModel的表


class User(AbstractUser):
    """
    用户信息
    """
    GENDER_CHOICES = (
        (1, '男'),
        (2, '女'),
    )

    GENDER_USERINFO_CHOICES = (
        (1, '男'),
        (2, '女'),
        (3, '保密'),
    )

    role_choices = (
        (0, "超级管理员"),
        (1, "普通管理员"),
        (2, "客服"),
        (3, "普通用户"),
    )

    active_status = (
        (0, '未启用'),
        (1, '已启用'),
    )
    nickname = models.CharField(max_length=64, null=True, blank=True, verbose_name="昵称")
    realname = models.CharField(max_length=64, null=True, blank=True, verbose_name='真实姓名')
    img_url = models.ImageField(max_length=256, null=True, blank=True, verbose_name="头像地址")
    mobile = models.CharField(max_length=11, verbose_name="手机号")
    gender = models.SmallIntegerField(choices=GENDER_CHOICES, default=1, verbose_name="性别")
    gender_userinfo = models.SmallIntegerField(choices=GENDER_USERINFO_CHOICES, default=1, verbose_name="用户个人中心性别")
    country = models.CharField(max_length=32, null=True, blank=True, verbose_name="国家")
    province = models.CharField(max_length=32, null=True, blank=True, verbose_name="省份")
    city = models.CharField(max_length=32, null=True, blank=True, verbose_name="城市")
    default_address = models.ForeignKey('Address', related_name='users', null=True, blank=True,
                                        on_delete=models.SET_NULL, verbose_name="默认地址")
    # 后台管理系统
    role = models.SmallIntegerField(choices=role_choices, blank=True, null=True, verbose_name="角色", default=3)
    active_status = models.SmallIntegerField(choices=active_status, default=1, verbose_name="角色状态")
    is_deleted = models.BooleanField(default=False, verbose_name="逻辑删除")

    class Meta:
        db_table = 'tb_user'
        verbose_name = "用户基本信息表"
        verbose_name_plural = verbose_name


class Address(BaseModel):
    """
    用户地址
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='addresses', verbose_name='用户')
    receiver = models.CharField(max_length=20, verbose_name="收件人", help_text="收件人")
    province = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='province_address',
                                 verbose_name="省")
    city = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='city_address', verbose_name="市")
    district = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='district_address',
                                 verbose_name="县")
    place = models.CharField(max_length=128, verbose_name="详细地址", help_text="详细地址")
    mobile = models.CharField(max_length=11, verbose_name="联系人", help_text="收件人电话")
    is_deleted = models.BooleanField(default=False, verbose_name="逻辑删除", help_text="逻辑删除")

    class Meta:
        db_table = "tb_address"
        verbose_name = "用户地址"
        verbose_name_plural = verbose_name
        ordering = ['-update_time']


class Feedback(BaseModel):
    """
    意见反馈
    """
    OPINION_CHOICES = (
        (1, '性能体验'),
        (2, '功能异常'),
        (3, '产品建议'),
        (4, '其他反馈'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='feedback', verbose_name='用户')
    suggestion_id = models.IntegerField(choices=OPINION_CHOICES, verbose_name="建议")
    suggestion_text = models.TextField(blank=True, null=True, verbose_name="反馈内容")

    class Meta:
        db_table = "tb_feedback"
        verbose_name = "意见反馈"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.realname
